FROM ubuntu:18.04

#检测操作系统架构
RUN umask 0022 \
    && useradd HwHiAiUser \
    && if [ "$(uname -a | grep aarch64)" != "" ] && [ ! -d "/lib64" ]; \
    then \
    mkdir /lib64 && ln -sf /lib/ld-linux-aarch64.so.1 /lib64/ld-linux-aarch64.so.1; \
    fi

#内网专用需配置代理
ENV http_proxy="proxyValue"
ENV https_proxy="proxyValue"
ENV ftp_proxy="proxyValue"

#内网专用，使用华为源
#<aptOri>RUN sed -i "s@http://archive.ubuntu.com@aptValue@g" /etc/apt/sources.list && \
#<aptOri>    sed -i "s@http://security.ubuntu.com@aptValue@g" /etc/apt/sources.list && \
#<aptOri>    sed -i "s@http://ports.ubuntu.com@aptValue@g" /etc/apt/sources.list

#安装系统依赖
RUN apt update && apt-get update && apt install -y \ 
    g++ \ 
    gfortran \
    iproute2 \
    iputils-ping \    
    libbz2-dev \
    libsqlite3-dev \
    libblas-dev \
    libblas3 \
    libopenblas-dev \   
    libssl-dev \
    libxslt1-dev \
    libffi-dev \
    make \ 
    net-tools \
    openssh-server \
    openssl \
    pciutils \
    sudo \
    unzip \
    vim \
    wget \
    xz-utils \
    zlib1g \
    zlib1g-dev \    
	cron \
    && apt clean \
    && rm -rf /var/lib/apt/lists/*

ENV WORK_SPACE="/home"
ENV TMP_SPACE="/tmp"
#设定工作目录
WORKDIR $WORK_SPACE

#安装cmake
ENV CMAKE_TMP_DIR="$TMP_SPACE/cmake-3.17.5"
COPY cmake-3.17.5.tar.gz $TMP_SPACE
RUN tar -zxf $TMP_SPACE/cmake-3.17.5.tar.gz -C $TMP_SPACE/ \
    && cd $CMAKE_TMP_DIR \
    && ./bootstrap --prefix=/usr \ 
    && make -j8 \
    && make install \
    && cd $TMP_SPACE \
    && rm -f $TMP_SPACE/cmake-3.17.5.tar.gz \
    && rm -rf $CMAKE_TMP_DIR

#安装python3.9.2
ENV PYTHON_TMP_DIR="$TMP_SPACE/Python-3.9.2"
COPY Python-3.9.2.tgz $TMP_SPACE/
RUN tar -zxf $TMP_SPACE/Python-3.9.2.tgz -C $TMP_SPACE/ \
    && cd $PYTHON_TMP_DIR \
    && ./configure --prefix=/usr/local/python3.9.2 --enable-shared \
    && make -j8 \
    && make install -j8 \
    && cp /usr/local/python3.9.2/lib/libpython3.9.so.1.0 /usr/lib \
    && ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python \
    && ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3 \
    && ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3.9 \
    && ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3.9.2 \
    && ln -sf /usr/local/python3.9.2/bin/pip3 /usr/bin/pip3.9 \
    && ln -sf /usr/local/python3.9.2/bin/pip3 /usr/bin/pip3.9.2 \
    && cd $PYTHON_TMP_DIR \
    && rm -f $TMP_SPACE/Python-3.9.2.tgz \
    && rm -rf $PYTHON_TMP_DIR

#安装python依赖
COPY pip_list.txt $TMP_SPACE

#外网用豆瓣源，内网不能用豆瓣源就用华为源
RUN pip3.9 install -U pip pipValue \
    && pip3.9 install -r $TMP_SPACE/pip_list.txt pipValue \
    && rm -rf ~/.cache/pip \
    && rm -f $TMP_SPACE/pip_list.txt

#设置 cann和SDK安装包名字及目录的环境变量
ARG NNRT_PKG=Ascend-cann-nnrt*.run
ARG SDK_PKG=Ascend-mindxsdk-mxmanufacture_*linux-*.run
ARG SDK_NAME=mxManufacture
ARG SDK_INSTALL_PATH=$WORK_SPACE

#设置系统依赖环境变量
ENV PATH=/usr/local/python3.9.2/bin:$PATH
ENV ASCEND_HOME=/usr/local/Ascend
ENV LD_LIBRARY_PATH=/usr/local/python3.9.2/lib:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/driver/lib64/common/:$ASCEND_HOME/driver/lib64/driver/:$LD_LIBRARY_PATH

#拷贝cann和工业质检SDK
COPY $NNRT_PKG $SDK_PKG ./

#安装cann
RUN chmod +x ${NNRT_PKG} \
    && ./${NNRT_PKG} --quiet --install --install-path=${ASCEND_HOME} --install-for-all \
    && rm ${NNRT_PKG}

#安装工业质检SDK
RUN . ${ASCEND_HOME}/nnrt/set_env.sh \
    && chmod +x ${SDK_PKG} \
    && ./${SDK_PKG} --install --install-path=${SDK_INSTALL_PATH} \
    && rm ${SDK_PKG}

#使用/bin/bash替换/bin/dash，才能使用set_env中的BASH_SOURCE变量
RUN ln -sf /bin/bash /bin/sh

#将环境变量写入bashrc
RUN echo ". ${ASCEND_HOME}/nnrt/set_env.sh" >> ~/.bashrc \
	&& echo ". ${WORK_SPACE}/mxManufacture/set_env.sh" >> ~/.bashrc

#加入推理服务包
ARG INFER_BAG=Ascend-mindxsdk-mxserving*.run
COPY ${INFER_BAG} /tmp
RUN cp ${TMP_SPACE}/${INFER_BAG} ${WORK_SPACE}
RUN chmod +x ${WORK_SPACE}/${INFER_BAG} && \
    ${WORK_SPACE}/${INFER_BAG} --install && \
    rm ${WORK_SPACE}/${INFER_BAG} && \
    mv ${WORK_SPACE}/mxAOIService ${WORK_SPACE}/ai_inference_atlas_sdk

#清除代理
ENV http_proxy=
ENV https_proxy=
ENV ftp_proxy=

#修改config.yaml request配置
COPY modify_infer_config.py ${WORK_SPACE}
RUN python3 ${WORK_SPACE}/modify_infer_config.py && rm ${WORK_SPACE}/modify_infer_config.py

#将推理服务flask作为容器启动的主进程,可使用docker run的cmd替换掉此CMD
CMD . ${ASCEND_HOME}/nnrt/set_env.sh \
    && . ${WORK_SPACE}/mxManufacture/set_env.sh \
    && cd /home/ai_inference_atlas_sdk/ \
    && python3.9.2 ./scripts/infer_service_generation.py \
    && ${WORK_SPACE}/ai_inference_atlas_sdk/start.sh -i 0.0.0.0 -s false -c true -u true -m true

